package main

import (
	"encoding/hex"
	"fmt"
	"signal/bll"
	"signal/model"
	"signal/utils"
	"strings"
)

type packetsInfo struct {
	name   string
	hexStr string
}

type multiPacketsType struct {
	packets    []packetsInfo
	parseCount int
}

func main() {

	multiPackets := []multiPacketsType{
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "登录报文0x01",
		//			hexStr: "AA140001014253433231303030313346303030313344",
		//		},
		//		{
		//			name:   "登录报文0x03",
		//			hexStr: "AA14000103B23BE83341CC824ABDC9F71FE21AE56CD0",
		//		},
		//		{
		//			name:   "登录报文0x03",
		//			hexStr: "AA1400811B023E66EB8D580185CACA0600004C520089",
		//		},
		//		{
		//			name:   "心跳报文",
		//			hexStr: "AA030003D7",
		//		},
		//		{
		//			name:   "解析错误的报文(第三个字节00错误，应该是AA才是正确报文)",
		//			hexStr: "AA14000103B23BE83341CC824ABDC9F71FE21AE56C49",
		//		},
		//	},
		//	parseCount: 50,
		//},
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "回复报文(0x61)",
		//			hexStr: "7E04026100A2",
		//		},
		//	},
		//	parseCount: 1,
		//},
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "混合两种报文(1:AA14000103B23BE83341CC824ABDC9F71FE21AE56CD0,2:7E04026100A2,3:AA030003D7)",
		//			hexStr: "AA14000103B23BE83341CC824ABDC9F71FE21AE56CD07E04026100A2AA030003D7",
		//		},
		//	},
		//	parseCount: 60,
		//},
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "ota报文",
		//			hexStr: "a55a1a0082b530d73391d0060101000000000846010000000000884b9c98",
		//		},
		//	},
		//	parseCount: 1,
		//},
		{
			packets: []packetsInfo{
				{
					name:   "两个回复报文(7E0B6253606158014B09CB06F0,7E0B3871606158014B09CB0642)",
					hexStr: "7E0B6253606158014B09CB06F07E0B3871606158014B09CB0642",
				},
			},
			parseCount: 1,
		},
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "登录报文0x03",
		//			hexStr: "AA14000103B23BE83341CC824ABDC9F71FE21AE56CD0",
		//		},
		//	},
		//	parseCount: 2,
		//},
		//{
		//	packets: []packetsInfo{
		//		{
		//			name:   "解析错误的报文(第三个字节00错误，应该是AA才是正确报文)",
		//			hexStr: "AA14000103B23BE83341CC824ABDC9F71FE21AE56C49",
		//		},
		//	},
		//	parseCount: 1,
		//},
	}

	bll.Session.Init()
	var rets []interface{}
	for _, m := range multiPackets {
		var str []string
		for _, mm := range m.packets {
			str = append(str, mm.hexStr)
		}

		if packetBytes, err := hex.DecodeString(strings.Join(str, "")); err != nil {
			fmt.Println(fmt.Sprintf("合并多个包错误:%+v", err))
		} else {
			h := bll.Handler
			parseCount := m.parseCount
			if parseCount > len(packetBytes) {
				parseCount = len(packetBytes)
			}
			bs := utils.SplitBytes(packetBytes, parseCount)
			for _, vv := range bs {
				fmt.Printf("待解析的数据报文:%X\n", vv)
				r, p, e := h.Parse(1, vv)
				if e != nil {
					fmt.Println(e)
					continue
				}
				fmt.Printf("解析错误的报文:%X\n", p)
				if len(r) > 0 {
					rets = append(rets, r...)
				}
			}
		}
	}
	fmt.Println("拆包的结果:")
	for _, v := range rets {
		if _, ok := v.(*model.Packet); ok {
			fmt.Printf("%X\n", v.(*model.Packet).ToHexBytes())
		}
		if _, ok := v.(*bll.PacketBSC22); ok {
			fmt.Printf("%X\n", v.(*bll.PacketBSC22).ToHexBytes())
		}
	}
	fmt.Println(rets)
	return
}
